<?php
namespace Home\Controller;
use Think\Controller;

/**
 * 用户控制
 */
class UserController extends Controller {
	//全局变量区
	/**
     * 布局相关
     * list_each_page_number：每一页列表条数
     */
    public $list_each_page_number = 5;
	/**
	 * 改变用户信息结果
	 * 0：改变成功
	 * 1：改变失败
	 */
	public $change_information_success = 0;
	public $change_information_false = 1;
	/**
	 * 改变密码结果
	 * 0：改变成功
	 * 1：旧密码错误
	 * 2：改变失败
	 */
	public $change_password_success = 0;
	public $change_password_old_err = 1;
	public $change_password_failed = 2;

	/**
	 * 进入用户主页
	 */
	public function index(){
		$this->assign('user_name', session('name'));
		$this->display();
	}

	//**********个人资料部分**********//

	/**
	 * 展示用户信息
	 */
	public function showUserInfo(){
		// 实例化用户对象
        $user = M('user_table');

        $where['id'] = session('id');
        $result = $user->where($where)->field('name, last_login_time, gender, email, phone, user_picture')->find();
        $this->ajaxReturn($result);
	}

	/**
	 * 更改用户信息
	 */
	public function changeInformation(){
		// 判断提交方式
        if (IS_POST) {
            // 实例化用户对象
            $user = M('user_table');

            // 自动验证 创建数据集
            /* if (!$data = $user->create()) {
                // 防止输出中文乱码
                header("Content-type: text/html; charset=utf-8");
                exit($user->getError());
            } */

            //更改数据
           /*  $changeData['name'] = $data['name'];
            $changeData['gender'] = $data['gender'];
            $changeData['email'] = $data['email'];
            $changeData['phone'] = $data['phone']; */
			$changeData['name'] = $_POST['name'];
            $gender=$_POST['gender'];
			if  ($gender=="男") {$changeData['gender'] =0;}
			else if ($gender=="女") {$changeData['gender'] =1;}
			else {$changeData['gender']="";}
            $changeData['email'] =$_POST['email'];
            $changeData['phone'] =$_POST['phone'];
            $where = array();
            $where['id'] = session('id');
            $changeResult = $user->where($where)->save($changeData);

            if($changeResult){
            	session('name', $changeData['name']);
            	$resultList['changeInformationResult'] = $this->change_information_success;
            }else{
            	$resultList['changeInformationResult'] = $this->change_information_false;
            }
            $this->ajaxReturn($resultList);
        }
	}

	/**
	 * 改变用户密码
	 */
	public function changePassword(){
		// 实例化Login对象
		$user = M('user_table');

		//从前端获取数据
		$userId = session('id');
		$oldPassword = $_POST['oldPassword'];
		$newPassword['password']= $_POST['newPassword'];

		//判别旧密码
		$result = $user->where("id=%s",$userId)->find();
		if($result['password'] == $oldPassword){
			$changeResult = $user->where("id=%s",$userId)->save($newPassword);
			if($changeResult){
				session('password', $newPassword['password']);
				$resultList['changeInformationResult'] = $this->change_password_success;
            }else{
				//$resultList['changeInformationResult'] = $newPassword['password'];
            	$resultList['changeInformationResult'] = $this->change_password_failed;
            }
		}else{
			//$resultList['changeInformationResult'] = $oldPassword;
			$resultList['changeInformationResult'] = $this->change_password_old_err;
		}

		$this->ajaxReturn($resultList);
	}

	//**********已加球会部分**********//

	/**
	 * 列表已加入球会
	 */
	public function listJoinedClub(){
		//获取session信息
		$user_id = session('id');

		//实例化球会成员对象
		$clubMember = M('club_member_table');
		//实例化球会对象
		$club = M('club_table');

		//位置信息
		$e_page_num = $this->list_each_page_number;  //每一页的球馆条数
		$page_num = $_POST['page_num'];  //页数

		//置位
		$start_p = $e_page_num * ($page_num - 1);

		//查询已加入球会
		$where['member_status'] = 2;
		$where['member_id'] = $user_id;
		$result = $clubMember->where($where)->field('club_id')->limit($start_p, $e_page_num)->select();

		//组装返回信息
		$resultList = array();
		for($x1 = 0; $x1 < count($resultList); $x1++){
			$clubId = $result[$x1];
			$clubInfo = array();
			unset($where);
			$where['id'] = $clubId;
			$clubInfo = $club->where($where)->find();
			$resultList[$x1] = $clubInfo;
		}

		$this->ajaxReturn($resultList);
	}

	/**
	 * 点击进入球会首页
	 */
	public function enterClub(){
		//从前端获取球会id
		$clubId = $_POST['club_id'];

		$this->assign('club_id', $clubId);
		$this->display('club/club_home');
	}

	//**********页面用户控制**********//

	/**
	 * 查询session
	 */
	public function getSessionExist(){
		$id = session('id');

		if($id){
			$resultList['session_exist'] = 1;
		}else{
			$resultList['session_exist'] = 0;
		}
		$this->ajaxReturn($resultList);
	}
}